package com.wframe.project.former.dataservices.coupon;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.frame.database.JBaseService;
import com.frame.database.JServiceFactory;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.frame.database.util.ConditionMap;
import com.frame.pageform.ClientException;

public class CouponService extends JBaseService<CouponDao> {

    public static CouponService getIntance() {
        return JServiceFactory.createJServiceProxy(CouponService.class);
    }

    public PageResult queryCoupon(int size, int pageno) throws Exception {
        ConditionMap cond = new ConditionMap();
        cond.put("state<>2");
        cond.put("tag=0");
        cond.addOrder("id", false);
        return getDao().queryPage(cond, size, pageno);
    }

    public PageResult queryCoupon(JdataBean parms, int size, int pageno) throws Exception {
        return getDao().queryPage(parms, size, pageno);
    }

    /**
     * 发送优惠劵
     *
     * @param id
     * @param userids
     * @throws Exception
     */
    public void send(String id, String[] userids) throws Exception {
        JdataBean coupon = getDao().getBean(id);
        int diff = coupon.getInt("amount") - coupon.getInt("sales");
        if (userids.length > diff) throw new ClientException("已超过发放数量");
        coupon.put("tag", 1);
        coupon.put("state", 1);
        coupon.put("get_couponid", id);
        coupon.put("get_source", 2);
        coupon.put("create_time", new Date());
        for (String userid : userids) {
            coupon.remove("id");
            coupon.put("userid", userid);
            coupon.put("code", createCode());
            getDao().saveCoupon(coupon.getDataMap());
        }
        getDao().addSales(id, userids.length);
    }

    /**
     * 创建优惠劵编码
     *
     * @return
     */
    public String createCode() {
        int maxlen = 12;
        String code = "";
        Random random = new Random();
        boolean exist = true;
        while (exist) {
            exist = false;
            code = "";
            for (int i = 0; i < maxlen; i++)
                code += String.valueOf(random.nextInt(9));
            try {
                JdataBean b = getDao().getSession().getObject("select id from wb_coupon where code=?", new Object[]{code});
                if (b != null) exist = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return code;
    }

    public void saveCoupon(Map dataMap) throws Exception {
        getDao().saveCoupon(dataMap);
    }

    public JdataBean getCoupon(String id) throws Exception {
        return getDao().getCoupon(id);
    }

    public PageResult queryCouponDetails(JdataBean parms, int pageSize, int pageNum) throws Exception {
        return getDao().queryCouponDetails(parms, pageSize, pageNum);
    }

    public List<JdataBean> queryCouponDetails(JdataBean parms) throws Exception {
        return getDao().queryCouponDetails(parms);
    }
}
